﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listStaticSegmentsOutput : Api_BaseOutput
   {
      public List<listStaticSegmentsResults> result { get; set; }
      public listStaticSegmentsParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listStaticSegmentsOutput( listStaticSegmentsInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<listStaticSegmentsResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Static Segments: {0}", this.result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_listStaticSegments[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            List<listStaticSegmentsResults> results = new List<listStaticSegmentsResults>();
            IEnumerable<XElement> segments = xml.Elements( "struct" );
            foreach ( XElement segment in segments )
            {
               int it; DateTime dt;
               listStaticSegmentsResults entry = new listStaticSegmentsResults();
               int.TryParse( segment.Element( "id" ).Value, out it );
               entry.id = it;
               entry.name = segment.Element( "name" ).Value;
               int.TryParse( segment.Element( "member_count" ).Value, out it );
               entry.member_count = it;
               DateTime.TryParse( segment.Element( "created_date" ).Value, out dt );
               entry.created_date = dt;
               DateTime.TryParse( segment.Element( "last_update" ).Value, out dt );
               entry.last_update = dt;
               DateTime.TryParse( segment.Element( "last_reset" ).Value, out dt );
               entry.last_reset = dt;
               //
               results.Add( entry );
            }
            this.result = results;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listStaticSegments[] JsonResult = jss.Deserialize<x_listStaticSegments[]>( this.api_Response );
               this.result = FormatResults( JsonResult ); ;
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }

      }

      // -------------------------------------------------------------------------
      private List<listStaticSegmentsResults> FormatResults( x_listStaticSegments[] results_array )
      {
         List<listStaticSegmentsResults> results = new List<listStaticSegmentsResults>();
         foreach ( x_listStaticSegments x_result in results_array )
         {
            DateTime dt;
            listStaticSegmentsResults item = new listStaticSegmentsResults();
            //
            item.id = x_result.id;
            item.name = x_result.name;
            item.member_count = x_result.member_count;
            DateTime.TryParse( x_result.created_date, out dt );
            item.created_date = dt;
            DateTime.TryParse( x_result.last_update, out dt );
            item.last_update = dt;
            DateTime.TryParse( x_result.last_reset, out dt );
            item.last_reset = dt;
            //
            results.Add( item );
         }
         //
         return results;
      }

      #endregion
   }
}
